package com.huobao.yunying.activitys;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.huobao.yunying.activitys_hehuoren.Act_Hehuoren;
import com.huobao.yunying.activitys_log_reg.Act_Login;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.fragments.Fragment_Fenlei1;
import com.huobao.yunying.fragments.Fragment_FristPage;
import com.huobao.yunying.fragments.Fragment_Gouwuche;
import com.huobao.yunying.fragments.Fragment_Mine;
import com.huobao.yunying.fragments.Fragment_MuyingQuan;
import com.huobao.yunying.fragments.Fragment_Woek;
import com.huobao.yunying.utils.SessionUtils;
import com.huobao.yunying.utils.Utils;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.ExitUtil;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

public class Act_Main extends HuobaoBaseActivity implements OnPageChangeListener, OnCheckedChangeListener {
	@ViewInject(R.id.viewpager)
	private ViewPager viewpager;
	@ViewInject(R.id.radiogroup)
	private RadioGroup radiogroup;
	@ViewInject(R.id.rb_shouye)
	private RadioButton rb_shouye;
	@ViewInject(R.id.rb_fenlei)
	private RadioButton rb_fenlei;
	@ViewInject(R.id.rb_jhc)
	private RadioButton rb_jhc;
	@ViewInject(R.id.rb_wode)
	private RadioButton rb_wode;
	@ViewInject(R.id.rb_muyingquan)
	private RadioButton rb_muyingquan;


	@Override
	protected int getContentView() {
		return R.layout.layout_main;
	}

	@Override
	protected String getTitleText() {
		return null;
	}

	List<Fragment> mFragments;

	@Override
	protected void init() {
		int role = getIntent().getIntExtra("role", 0);
//		PgyFeedbackShakeManager.register(this);
//		PgyUpdateManager.register(this);
		final int select = getIntent().getIntExtra("select", 0);

		EventBus.getDefault().register(this);
		mAbTitleBar.setVisibility(View.GONE);

		mFragments = new ArrayList<Fragment>();

		if (role == 2)
			initStoreTab();
		else
			initNomalTab();
		checkUpdate();
	}


	private void initNomalTab() {
		LogUtil.e("initNomalTab");
		mFragments.clear();
		Fragment_FristPage fragment1 = new Fragment_FristPage();
		Fragment_Fenlei1 fragment2 = new Fragment_Fenlei1();
		Fragment_Gouwuche fragment3 = new Fragment_Gouwuche();
		Fragment_Mine fragment4 = new Fragment_Mine();
		Fragment_MuyingQuan fragment = new Fragment_MuyingQuan();

		mFragments.add(fragment1);
		mFragments.add(fragment2);
		mFragments.add(fragment);
		mFragments.add(fragment3);
		mFragments.add(fragment4);
		rb_shouye.setChecked(true);
		viewpager.setOffscreenPageLimit(4);
		viewpager.setOnPageChangeListener(this);
		radiogroup.setOnCheckedChangeListener(this);
		viewpager.setAdapter(new Adapter(getSupportFragmentManager()));
	}

	private void initStoreTab() {
		LogUtil.e("initStoreTab");
		mFragments.clear();
		Fragment_FristPage fragment1 = new Fragment_FristPage();
		Fragment_Fenlei1 fragment2 = new Fragment_Fenlei1();
		Fragment_Gouwuche fragment3 = new Fragment_Gouwuche();
		Fragment_Woek fragment4 = new Fragment_Woek();
		Fragment_MuyingQuan fragment = new Fragment_MuyingQuan();

		mFragments.add(fragment1);
		mFragments.add(fragment2);
		mFragments.add(fragment);
		mFragments.add(fragment3);
		mFragments.add(fragment4);
		rb_shouye.setChecked(true);
		viewpager.setOffscreenPageLimit(4);
		viewpager.setOnPageChangeListener(this);
		radiogroup.setOnCheckedChangeListener(this);
		viewpager.setAdapter(new Adapter(getSupportFragmentManager()));
	}


	private void checkUpdate() {
		Utils.checkUpdate(this,false);
	}

	//（1、普通会员，2、店铺，3合伙人）
	@Subscriber(tag = "LoginSuccess")
	public void LoginSuccess(int userRole) {
		switch (userRole) {
			case 1:
				initNomalTab();
				break;
			case 2:
				initStoreTab();
				break;
			case 3:
				Intent intent = new Intent(this, Act_Hehuoren.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				rb_shouye.setChecked(true);
				viewpager.setCurrentItem(0);
				break;
		}
	}

	@Subscriber(tag = "skipToMine")
	public void skipToMine(String str) {
		viewpager.setCurrentItem(4);
		rb_wode.setChecked(true);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}

	@Subscriber(tag = "GetUserMessageFail")
	public void GetUserMessageFail(String str) {
		viewpager.setCurrentItem(0);
		rb_shouye.setChecked(true);
		Intent intent = new Intent(this, Act_Login.class);
		startActivityForResult(intent, 1);
	}

	@Subscriber(tag = "ChangeTag")
	public void ChangeTag(String tag) {
		viewpager.setCurrentItem(Integer.parseInt(tag));
		rb_fenlei.setChecked(true);
	}

	@Subscriber(tag = "ShowGouwuche")
	public void ShowGouwuche(String str) {
		viewpager.setCurrentItem(3);
		rb_jhc.setChecked(true);
	}

	@Subscriber(tag = "goFristPage")
	public void goFristPage(String str) {
		radiogroup.setVisibility(View.VISIBLE);
		rb_shouye.setChecked(true);
		viewpager.setCurrentItem(0);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int arg0) {
		switch (arg0) {
			case 0:
				radiogroup.setVisibility(View.VISIBLE);
				rb_shouye.setChecked(true);
				break;
			case 1:
				radiogroup.setVisibility(View.VISIBLE);
				rb_fenlei.setChecked(true);
				break;
			case 2:
				radiogroup.setVisibility(View.VISIBLE);
				rb_muyingquan.setChecked(true);
				break;
			case 3:
//				radiogroup.setVisibility(View.GONE);
				rb_jhc.setChecked(true);
				break;
			case 4:
				radiogroup.setVisibility(View.VISIBLE);
				if (SessionUtils.checkLogin(context) == false) {
					Intent intent = new Intent(this, Act_Login.class);
					startActivity(intent);
					rb_shouye.setChecked(true);
					viewpager.setCurrentItem(0);
					return;
				}
				rb_wode.setChecked(true);
				break;
		}
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
			case R.id.rb_shouye:
				radiogroup.setVisibility(View.VISIBLE);
				viewpager.setCurrentItem(0);
				break;
			case R.id.rb_fenlei:
				radiogroup.setVisibility(View.VISIBLE);
				viewpager.setCurrentItem(1);
				break;
			case R.id.rb_muyingquan:
				viewpager.setCurrentItem(2);
				radiogroup.setVisibility(View.VISIBLE);
				break;
			case R.id.rb_jhc:
				viewpager.setCurrentItem(3);
//				radiogroup.setVisibility(View.GONE);
				break;
			case R.id.rb_wode:
				radiogroup.setVisibility(View.VISIBLE);
				if (SessionUtils.checkLogin(context) == false) {
					viewpager.setCurrentItem(0);
					rb_shouye.setChecked(true);
					Intent intent = new Intent(this, Act_Login.class);
					startActivityForResult(intent, 1);
					break;
				}
				viewpager.setCurrentItem(4);
				break;
		}
	}

	private class Adapter extends FragmentPagerAdapter {

		public Adapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			return mFragments.get(arg0);
		}

		@Override
		public int getCount() {
			return mFragments.size();
		}

	}


	private long tempTime = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			LogUtil.e("onKeyDown------------------------------");
			if (radiogroup.getVisibility() == View.GONE) {
				goFristPage(null);
				return false;
			}
			if (tempTime == 0) {
				tempTime = System.currentTimeMillis();
				Toast.makeText(context, "再按一次退出程序!", Toast.LENGTH_SHORT).show();
				return false;
			}
			if ((System.currentTimeMillis() - tempTime) <= 2 * 1000) {
				//退出程序-
				ExitUtil.getInstant().exitApp(context);
				finish();
				return false;
			}
			Toast.makeText(context, "再按一次退出程序!", Toast.LENGTH_SHORT).show();
			tempTime = System.currentTimeMillis();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
}
